import 'package:mvvm_provider/page/home/data/model/cartoon_model.dart';
import 'package:mvvm_provider/until/app_util.dart';
import 'package:mvvm_provider/until/base_db_manager.dart';
import 'package:mvvm_provider/until/date_util.dart';
import 'package:sqflite/sqlite_api.dart';

/// 收藏小说数据

class NovelDbManager extends BaseDbManager {
  /// 表名
  final String name = 'novel_info';

  /// 子id
  final String itemId = "item_id";

  /// 父id
  final String parentId = "parent_id";

  /// 图片链接
  final String image = "image";

  /// 标题
  final String title = "title";

  /// 收藏时间
  final String createTime = "create_time";

  static NovelDbManager? _instance;

  static NovelDbManager getInstance() => _instance ??= NovelDbManager();

  @override
  tableName() {
    return name;
  }

  /// 创建表
  @override
  createTableString() {
    return '''
        create table $name (
        $itemId text not null,$parentId text not null,
        $image text not null,$title text not null,$createTime text not null)
      ''';
  }

  /// 查询
  Future<List?> _getNoteProvider(Database db, String id) async {
    List<Map<String, dynamic>> maps = await db.rawQuery("select * from $name where $itemId = $id");
    return maps;
  }

  /// 插入
  Future insert(CartoonModel model) async {
    Database db = await getDataBase();
    var maps = await _getNoteProvider(db, model.itemId ?? '');
    if (AppUtil.isNotEmpty(maps)) {
      /// 更新数据
      await update(model);
    }

    return await db.rawInsert(
        "insert into $name ($itemId,$parentId,$image,$title,$createTime) values (?,?,?,?,?)", [
      model.itemId,
      model.parentId,
      model.image,
      model.title,

      /// 当前时间戳
      DateUtil.getNowTimeStr(),
    ]);
  }

  /// 删除
  Future delete(String id) async {
    Database db = await getDataBase();
    var maps = await _getNoteProvider(db, id);
    if (maps != null) {
      /// 删除数据
      await db.delete(name, where: "$itemId = ?", whereArgs: [id]);
    }
    return await db.rawDelete(
      "delete from $name where $itemId = $id",
    );
  }

  /// 更新
  Future update(CartoonModel model) async {
    Database database = await getDataBase();
    await database.rawUpdate(
        "update $name set $itemId = ?,$parentId = ?,$image = ?,$title = ?,$createTime = ?, where $itemId = ?",
        [
          model.itemId,
          model.parentId,
          model.image,
          model.title,
          DateUtil.getNowTimeStr(),
          model.itemId
        ]);
  }

  /// 查询单条数据
  Future<CartoonModel?> queryItem(String id) async {
    Database database = await getDataBase();
    var maps = await _getNoteProvider(database, id);
    if (AppUtil.isNotEmpty(maps)) {
      return CartoonModel.fromJson(maps![0]);
    }
    return null;
  }

  /// 查询全部数据
  Future<List<CartoonModel>?> queryAllData() async {
    Database database = await getDataBase();
    List<Map<String, dynamic>> maps = await database.rawQuery("select * from $name");
    if (AppUtil.isNotEmpty(maps)) {
      return maps.asMap().keys.map((index) => CartoonModel.fromJson(maps[index])).toList();
    }
    return null;
  }
}
